/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.business.sec.rbac.db;

import han.bifang.business.dao.GroupDao;
import han.bifang.business.dao.UserDao;
import han.bifang.business.dao.UserGroupRelDao;
import han.bifang.business.sec.rbac.UserProvider;
import han.bifang.business.sync.SyncUtils;
import han.bifang.common.entity.SoaUser;
import han.bifang.common.entity.SoaUsergroup;
import han.bifang.common.entity.SoaUsergroupUserRel;
import han.bifang.common.entity.SoaUsergroupUserRelPK;
import han.bifang.common.exception.BifangException;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author Kent
 */
public class DbUserProvider implements UserProvider {

	@Override
	public String createUser(SoaUser user) throws BifangException {
		
		return UserDao.createUser(user);
	}

	public List findUsersByPage(String loginName, String userName,int page, int pageSize)
			throws BifangException {
		return UserDao.findUsersByPage(loginName, userName, page, pageSize);
	}

	@Override
	public List<SoaUsergroup> getGroupsOfUser(String userId)
			throws BifangException {
		return GroupDao.findGroupByUserId(userId);
	}

	@Override
	public SoaUser getUserByLoginName(String loginName) throws BifangException {
		return UserDao.getUserByLoginName(loginName);
	}

	@Override
	public void modifyUser(SoaUser user) throws BifangException {
		UserDao.modifyUser(user);
		SyncUtils.sync(SyncUtils.SECURITY, null);

	}

	@Override
	public void removeUser(String userId) throws BifangException {
		UserDao.removeUser(userId);
		UserGroupRelDao.removeUsergroupUserRelByUserId(userId);
		SyncUtils.sync(SyncUtils.SECURITY, null);

	}

	@Override
	public SoaUser getUserById(String id) throws BifangException {
		return UserDao.getUserById(id);
	}

	@Override
	public void addGroupsOfUser(String[] groupIds, String userId)
			throws BifangException {
		ArrayList<SoaUsergroupUserRel> l = new ArrayList<SoaUsergroupUserRel>();
		for(String groupId : groupIds){
			SoaUsergroupUserRel r = new SoaUsergroupUserRel();
			SoaUsergroupUserRelPK rk = new SoaUsergroupUserRelPK();
			rk.setUsergroupId(groupId);
			rk.setUserId(userId);
			r.setCompId(rk);
			l.add(r);
		}
		UserGroupRelDao.createUsergroupUserRel(l);
		SyncUtils.sync(SyncUtils.SECURITY, null);
		
	}

	@Override
	public void removeGroupsOfUser(String[] groupIds, String userId)
			throws BifangException {
		ArrayList<SoaUsergroupUserRel> l = new ArrayList<SoaUsergroupUserRel>();
		for(String groupId : groupIds){
			SoaUsergroupUserRel r = new SoaUsergroupUserRel();
			SoaUsergroupUserRelPK rk = new SoaUsergroupUserRelPK();
			rk.setUsergroupId(groupId);
			rk.setUserId(userId);
			r.setCompId(rk);
			l.add(r);
		}
		UserGroupRelDao.removeUsergroupUserRel(l);
		SyncUtils.sync(SyncUtils.SECURITY, null);
		
	}

	@Override
	public List<SoaUsergroup> getGroupsNotOfUserByPage(String userId,String groupName, int page,
			int pageSize) throws BifangException {
		return GroupDao.findGroupsNotOfUserByPage(userId, groupName,page, pageSize);
	}

}
